﻿using System;
using System.Collections.Generic;
using System.Text;
using XcoAppSpaces.Contracts.Service;
using NUnit.Framework;
using XcoAppSpaces.Contracts.Exceptions;
using XcoAppSpaces.Security.Basic;
using XcoAppSpaces.Contracts.Security;

namespace test.XcoAppSpaces.Security.Basic
{
    [TestFixture]
    public class testStringConfig
    {
        [Test]
        public void NoConfig_NoService_loaded()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            configStrings.Add("nothing", "nothing");
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            Assert.IsNotNull(serviceConfig);
            Assert.IsNull(serviceConfig.Service);
        }
        [Test]
        public void Config_Active_Correct_Service()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();           
            configStrings.Add(ConfigStringInterpreter.ConfigStore, "c:\tmp.dat");
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            Assert.IsNotNull(serviceConfig.Service);
            Assert.AreEqual(typeof(XcoBasicSecurityService), serviceConfig.Service.GetType());
            XcoBasicSecurityService service = (XcoBasicSecurityService)serviceConfig.Service;
        }
        [Test]
        public void Config_Policy_Correct_Only()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            string policy = XcoSecurityPolicy.Permissive.ToString();
            configStrings.Add(ConfigStringInterpreter.ConfigPolicy, policy);
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            XcoBasicSecurityService service = (XcoBasicSecurityService)serviceConfig.Service;
            Assert.AreEqual(service.Policy.ToString(), policy);
        }
        [Test]
        public void Config_Policy_Wrong_Only()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            configStrings.Add(ConfigStringInterpreter.ConfigPolicy, "myPolicy");
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            try
            {
                XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
                Assert.Fail("Missing XcoConfigurationException.");
            }
            catch (XcoConfigurationException)
            {
            }
        }
        [Test]
        public void Config_StorePath_Correct_Only()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            string storepath = @"c:\tmp.dat";
            configStrings.Add(ConfigStringInterpreter.ConfigStore, storepath);
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            XcoBasicSecurityService service = (XcoBasicSecurityService)serviceConfig.Service;
            Assert.AreEqual(service.StorePath, storepath);
        }
     
     

        [Test]
        public void Config_AllProperies()
        {
            Dictionary<string, string> configStrings = new Dictionary<string, string>();
            string storepath = @"c:\tmp.dat";           
            string policy = XcoSecurityPolicy.Permissive.ToString();
            configStrings.Add(ConfigStringInterpreter.ConfigPolicy, policy);
            configStrings.Add(ConfigStringInterpreter.ConfigStore, storepath);
            IXcoConfigStringInterpreter interpreter = new ConfigStringInterpreter();
            XcoServiceConfiguration serviceConfig = interpreter.Interpret(configStrings);
            XcoBasicSecurityService service = (XcoBasicSecurityService)serviceConfig.Service;
            Assert.AreEqual(service.Policy.ToString(), policy);
            Assert.AreEqual(service.StorePath, storepath);
        }
    }

}
